home *** CD-ROM | disk | FTP | other *** search
- Path: news.daimi.aau.dk!liborius
- From: liborius@daimi.aau.dk (Per Liboriussen)
- Newsgroups: comp.sys.amiga.programmer
- Subject: Re: 3 bugs in SAS/C v. 6.56
- Date: 26 Feb 1996 10:54:44 GMT
- Organization: DAIMI, Computer Science Dept. at Aarhus University
- Message-ID: <4gs3hk$7mp@gjallar.daimi.aau.dk>
- References: <4gjvvc$a3t@gjallar.daimi.aau.dk> <312EFB06.708D@diku.dk>
- NNTP-Posting-Host: drachma.daimi.aau.dk
-
- Thus spake Morten B=?iso-8859-2?Q?=F8geskov Jensen <bogeskov@diku.dk>?=:
-
- >#include <stdio.h>
-
- >int main(void)
- >{
- > unsigned short a = 0xffff, b = 0xffff;
-
- > if ((a | b) > (unsigned short)0x7fff) {
- > printf("ok\n");
- > } else {
- > printf("bad\n");
- > }
- > return 0;
- >}
-
- >This should work.. But I haven't tested it, since I'm not at home, so I
- >cannot reach out and get SAS/C.
-
- Neither am I, so the same goes for me. However, a working workaround is
- something like:
- ...
- unsigned short x = (a | b);
-
- if (x > 0x7fff) {
- ...
-
- The problem is not finding the workaround, it is finding the code that causes
- the problems.
-
- >However I belive that ">" and other boolean operations are
- >left-associative (I know), as so they take the type of the 2nd (right)
- >parameter, and since 0x7fff per default is signed, there is an implicit
- >type conversion of unsigned short to signed short. this is most
- >unfortunate, however this is the way most C-compilers work.
-
- I don't see what the associativeness of ">" has to do with the above.
- Anyway, when a compiler evaluates a boolean expression such as ">", it
- must perform the usual integral promotions. This means that types narrower
- than int are promoted to int before the comparison. Even in your example
- above the compiler must take the (integer) constant 0x7fff, convert it to
- unsigned short, and then immediately convert it back to int. Of course,
- the ISO C Standard does give permission for an implementation to carry out
- the comparison by any arcane magic it may wish, as long as a conforming
- program is not able to tell the difference. (The "as if" rule.)
-
- (Note furthermore that even though a and b are both unsigned shorts, the
- type of the expression (a | b) is int!)
-
- Since an int in this case is capable of representing all possible values
- of an unsigned short, the promotion of a and b to int must be
- value-preserving.
-
-
- >The other 2 examples I couldnt see why they wouldn't work.
-
- As the subject line says: Bugs. ;-)
-
-
- --
- Per Liboriussen
- liborius@daimi.aau.dk
-